#include "stan.h"


Stan::Stan(QGraphicsView *g, QVBoxLayout *listLayout)
{
    graphicsView=g;
    this->scene=g->scene();
    this->listLayout=listLayout;
    currentIndex=0;
    shapes.push_back(Shape("rMarker"));

    positions.push_back(QPointF(0,0));
    updateScene();


}
Stan::~Stan()
{
    shapes.clear();
    positions.clear();
    productionsList.clear();



}

int Stan::find(const QPointF& p)
{
    QPointF t,v;// t=shape position, v- marker position in shape

    Shape s;

    qDebug()<<"searching point "<<p;
    for(int i=0;i<positions.size();++i)
    {

        t=positions.at(i);
        qDebug()<<"checking point: "<<t;
        s=shapes.at(i);
        if(s.rMarker.exist)
        {
            v=s.rMarker.position;
            v.setY(-v.y());
            qDebug()<<"rmarker vec: "<<v;
            qDebug()<<"analysing point: ("<<t.x()+v.x()<<","<<t.y()+v.y()<<")";
            if(p.x()<=t.x()+v.x()+accuracy && p.x()>=t.x()+v.x()-accuracy && p.y()<=t.y()+v.y()+accuracy && p.y()>=t.y()+v.y()-accuracy)
            {
                qDebug()<<"point found: ("<<t.x()+v.x()<<","<<t.y()+v.y()<<")";
                qDebug()<<"point found: ("<<t.x()+v.x()<<","<<t.y()+v.y()<<")";
                return i;
            }
        }
        if(s.uMarker.exist)
        {
            v=s.uMarker.position;
            v.setY(-v.y());
            qDebug()<<"umarker vec: "<<v;
            qDebug()<<"analysing point: ("<<t.x()+v.x()<<","<<t.y()+v.y()<<")";
            if(p.x()<=t.x()+v.x()+accuracy && p.x()>=t.x()+v.x()-accuracy && p.y()<=t.y()+v.y()+accuracy && p.y()>=t.y()+v.y()-accuracy)
            {
                qDebug()<<"point found: ("<<t.x()+v.x()<<","<<t.y()+v.y()<<")";
                return i;
            }
        }


    }
    qDebug()<<"point not found!";
    return -1;
}

void Stan::sceneClicked(const QPointF & p)
{

    qDebug()<<"clicked: "<<p;
    currentIndex=find(p);
    if (currentIndex==-1)return;
    updateList(currentIndex);
}
void Stan::productionClicked(const RShape& r)
{
    this->shapes.removeAt(currentIndex);
    QPointF pos=positions.at(currentIndex);
    this->positions.removeAt(currentIndex);


    float min=0.0f;
    for(int i=0;i<r.positions.size();++i)
    {
        QPointF p=r.positions.at(i);
        if(pos.y()-p.y()<min)
            min=pos.y()-p.y();

        positions.push_back(QPointF(pos.x()+p.x(),pos.y()-p.y()));
        shapes.push_back(r.shapes.at(i));

    }



    min=-min;
    QPointF p;
    int N=positions.size();
    for(int i=0;i<N;++i)
    {
        p=positions.at(i);
        p.setY(p.y()+min);
        positions.replace(i,p);
    }

    scene->clear();
    updateScene();
    foreach(QGraphicsView *v, productionsList)
        delete v;
    productionsList.clear();



}

void Stan::updateList(int index)
{


        qDebug()<<"------------------List updated! -------------------";
        Shape s=shapes.at(index);
        qDebug()<<"setting shape:"<<s.name<<", markers: (r,u): "<<s.rMarker.exist<<" "<<s.uMarker.exist;
        qDebug()<<"finding productions:";



        QList<RShape> list;
        foreach(Production p,grammar.productions)
        {
            qDebug()<<"analysing production:"<<p.lshape.name<<" "<<p.lshape.rMarker.exist<<" "<<p.lshape.uMarker.exist;
            if(s.equal(p.lshape))
                list.push_back(p.rshape);

        }
        qDebug()<<list.count()<<" productions have been found!";



         int i=0;
        foreach(RShape r,list)
        {

            MyGraphicsView *view=new MyGraphicsView(this,r);

            view->setObjectName(QString().setNum(i));
            i++;
            productionsList.push_back(view);

            view->setAlignment(Qt::AlignTop|Qt::AlignLeft);
            listLayout->addWidget(view);

            view->setScene(new QGraphicsScene);
            view->adjustSize();
            view->show();
            view->installEventFilter(view);

            QGraphicsPixmapItem *item;
            QGraphicsEllipseItem *ellipse;

            double posx=0.0;
            double posy=0.0;
            //rysowanie prawych ksztaltow
            for(int j=0;j<r.positions.size();++j)
            {

                item=view->scene()->addPixmap(QPixmap::fromImage(r.shapes.at(j).image));
                double y=posy-r.positions.at(j).y();
                double x=posx+r.positions.at(j).x();
                item->translate(x,y);



            }

            //rysowanie markerow prawych ksztaltow
            for(int j=0;j<r.positions.size();++j)
            {
                double y=posy-r.positions.at(j).y();
                double x=posx+r.positions.at(j).x();
                Marker mu=r.shapes.at(j).uMarker;
                Marker mr=r.shapes.at(j).rMarker;

                if(mr.exist)
                {
                    ellipse=view->scene()->addEllipse(0,0,d,d);
                    ellipse->setBrush(QBrush(QColor(0,0,0)));
                    ellipse->translate(x+mr.position.x()-d/2,y-mr.position.y()-d/2);

                }

                if(mu.exist)
                {
                    ellipse=view->scene()->addEllipse(0,0,d,d);
                    ellipse->setBrush(QBrush(QColor(255,0,0)));
                    ellipse->translate(x+mu.position.x()-d/2,y-mu.position.y()-d/2);


                }
            }

        }

        qDebug()<<"---------------------------------------------------";


}
void Stan::updateScene()
{


    qDebug()<<"------------------Scene updated! -------------------";

    QGraphicsPixmapItem *item;
    QGraphicsEllipseItem *ellipse;
    QPointF p;
    Shape s;


    //rysowanie ksztaltow
    for(int i=0;i<positions.size();++i)
    {
        p=positions.at(i);
        s=shapes.at(i);
        item=scene->addPixmap(QPixmap::fromImage(s.image));
        item->translate(p.x(),p.y());

    }




    //rysowanie markerow ksztaltow
    for(int i=0;i<positions.size();++i)
    {
        p=positions.at(i);
        qDebug()<<"shape pos:"<<p;
        s=shapes.at(i);
        Marker u=s.uMarker;
        Marker r=s.rMarker;

        if(r.exist)
        {
            ellipse=scene->addEllipse(0,0,d,d);
            ellipse->setBrush(QBrush(QColor(0,0,0)));
            qDebug()<<"in shape: rpos: ( "<<p.x()+r.position.x()-d/2<<","<<p.y()-r.position.y()-d/2<<" )";
            ellipse->translate(p.x()+r.position.x()-d/2,p.y()-r.position.y()-d/2);
        }

        if(u.exist)
        {
            ellipse=scene->addEllipse(0,0,d,d);
            ellipse->setBrush(QBrush(QColor(255,0,0)));
            qDebug()<<"in shape: upos: ( "<<p.x()+u.position.x()-d/2<<","<<p.y()-u.position.y()-d/2<<" )";
            ellipse->translate(p.x()+u.position.x()-d/2,p.y()-u.position.y()-d/2);

        }

    }
     qDebug()<<"----------------------------------------------------";


}



